$(function () {
    let id = location.search.split("=")[1]
    let cid = $("[name=articleId]").val()
    console.log(cid);

    console.log(id);
    newComment();
    focusFollow();
    hotList();
    getContent();
    comment_list();
    cate();
    // 页面渲染
    function getContent() {
        $.ajax({
            url: '/index/article',
            type: 'get',
            data: {
                id
            },
            success: (res) => {
                console.log(res);
                let htmlStr = template("content", {
                    list: res.data
                })
                $(".content").html(htmlStr)
            }
        })
    }

    // 评论列表
    function comment_list() {
        $.ajax({
            url: '/index/get_comment',
            type: 'get',
            data: {
                articleId: id
            },
            success: (res) => {
                // console.log(res);
                let s = template("comment_list", {
                    list: res.data
                })
                $(".comment").html(s)
            }
        })
    }
    // 发表评论
    $(".comment_form").on("submit", function (e) {
        e.preventDefault();
        console.log(1);

        if ($('.comment_name').val().trim().length == 0 || $('.comment_input').val().trim().length == 0) {
            alert('请输入用户名和评论内容');
            return;
        };
        $.ajax({
            url: '/index/post_comment',
            type: 'post',
            data: {
                author: $('.comment_name').val(),
                content: $('.comment_input').val(),
                articleId: id
            },
            dataType: 'json',
            success: (res) => {
                // console.log(res);
                if (res.code == 200) {
                    alert('发表成功');
                    window.location.reload();
                }
            }
        })
    })

    // 一周热门排行
    function hotList() {
        $.ajax({
            url: '/index/rank',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);

                let htmlStr = template("hot_list", {
                    list: res.data
                })
                $(".hotrank_list").html(htmlStr)
                $(".hotrank_list li").eq(0).find("span").addClass("first")
                $(".hotrank_list li").eq(1).find("span").addClass("second")
                $(".hotrank_list li").eq(2).find("span").addClass("third")
            }
        })
    }

    // 最新评论
    function newComment() {
        $.ajax({
            url: '/index/latest_comment',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);
                let htmlStr = template("new_comment", {
                    list: res.data
                })
                $(".comment_list").html(htmlStr)
            }
        })
    }

    // 焦点关注
    function focusFollow() {
        $.ajax({
            url: '/index/attention',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);
                let htmlStr = template("focus_follow", res

                )
                $(".guanzhu_list").html(htmlStr)
            }
        })
    }
    // 分类 导航栏
    function cate() {
        $.ajax({
            url: '/index/category',
            type: 'get',
            data: {},
            success: (res) => {
                console.log(res);
                let htmlStr = template("cate", {
                    list: res.data
                })
                $(".level_two").html(htmlStr)
                $(".left_menu").html(htmlStr)
            }
        })
    }
})